package com.ff.xyh.access.service.impl.post;

import com.ff.xyh.access.service.access.AccessUserService;
import com.ff.xyh.access.service.post.PostShopFeignService;
import com.ff.xyh.common.entity.dictionary.CheckStatus;
import com.ff.xyh.common.entity.dictionary.PlatformDomain;
import com.ff.xyh.common.entity.dictionary.PostStatus;
import com.ff.xyh.common.entity.post.PostImg;
import com.ff.xyh.common.entity.post.PostShop;
import com.ff.xyh.common.entity.post.PostTravel;
import com.ff.xyh.common.entity.result.ResultCode;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.entity.useraccess.AccessUser;
import com.ff.xyh.common.qo.post.PostShopQuery;
import com.ff.xyh.common.utils.CommonUtil;
import com.ff.xyh.common.vo.post.PostShopBaseInfo;
import com.ff.xyh.common.vo.post.PostShopVo;
import com.ff.xyh.common.vo.user.UserBaseInfo;
import com.ff.xyh.feign.client.dictionary.CheckStatusClient;
import com.ff.xyh.feign.client.dictionary.PlatformDomainClient;
import com.ff.xyh.feign.client.dictionary.PostStatusClient;
import com.ff.xyh.feign.client.post.PostClient;
import com.ff.xyh.feign.client.post.PostImgClient;
import com.ff.xyh.feign.client.userfront.FrontUserClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther FF
 * @Date 2023年 05月 25日
 * @Time 03:12
 */
@Service
public class PostShopFeignServiceImpl implements PostShopFeignService {

    @Autowired
    PostClient postClient;

    @Autowired
    PostImgClient postImgClient;

    @Autowired
    FrontUserClient frontUserClient;

    @Autowired
    PostStatusClient postStatusClient;

    @Autowired
    CheckStatusClient checkStatusClient;

    @Autowired
    PlatformDomainClient platformDomainClient;

    @Autowired
    AccessUserService accessUserService;

    @Override
    public ResultObject getPostShopCondition(PostShopQuery postShopQuery) {
        ResultObject resultObject = postClient.getPostShopPageCondition(postShopQuery);

        if (!resultObject.getCode().equals(ResultCode.SUCCESS_CODE))
            return resultObject;

        List<PostShop> list = CommonUtil.getItemsInResultObject(resultObject, PostShop.class);
        if (list == null || list.isEmpty()) return ResultObject.ok();

        List<PostStatus> postStatusList =
                CommonUtil.getItemsInResultObject(postStatusClient.getAllPostStatus(), PostStatus.class);
        List<CheckStatus> checkStatusList =
                CommonUtil.getItemsInResultObject(checkStatusClient.getAllCheckStatus(), CheckStatus.class);
        List<PlatformDomain> platformDomainList =
                CommonUtil.getItemsInResultObject(platformDomainClient.getAllPlatformDomain(), PlatformDomain.class);

        List<String> ids = list.stream().map(PostShop::getUserId).collect(Collectors.toList());
        List<UserBaseInfo> listUsers =
                CommonUtil.getItemsInResultObject(frontUserClient.getUserBaseInfosByIds(ids), UserBaseInfo.class);

        List<String> checkerIds = list.stream().map(PostShop::getCheckerId).collect(Collectors.toList());
        Collection<AccessUser> listCheckers = accessUserService.listByIds(checkerIds);

        List<PostShopVo> voList = new ArrayList<>();
        list.forEach(postShop -> {
            PostShopVo postShopVo = new PostShopVo();
            PostShopBaseInfo postShopBaseInfo = new PostShopBaseInfo();
            BeanUtils.copyProperties(postShop, postShopBaseInfo);

            if (listUsers != null && !listUsers.isEmpty()) {
                // 用户
                for (UserBaseInfo userBaseInfo : listUsers) {
                    if (userBaseInfo.getId().equals(postShop.getUserId())) {
                        postShopBaseInfo.setUserId(userBaseInfo.getId());
                        postShopBaseInfo.setUserNickname(userBaseInfo.getNickname());
                        postShopBaseInfo.setUserAvatar(userBaseInfo.getAvatar());
                    }
                }
            }

            if (postStatusList != null && !postStatusList.isEmpty()) {
                // 帖子状态
                for (PostStatus postStatus : postStatusList) {
                    if (postStatus.getId().equals(postShop.getPostStatus())) {
                        postShopBaseInfo.setStatus(postStatus.getStatus());
                        break;
                    }
                }
            }
            if (checkStatusList != null && !checkStatusList.isEmpty()) {
                // 审核状态
                for (CheckStatus checkStatus : checkStatusList) {
                    if (checkStatus.getId().equals(postShop.getCheckStatus())) {
                        postShopBaseInfo.setCheckStatus(checkStatus.getStatus());
                        break;
                    }
                }
            }
            if (platformDomainList != null && !platformDomainList.isEmpty()) {
                // 平台
                for (PlatformDomain platformDomain : platformDomainList) {
                    if (platformDomain.getId().equals(postShop.getPlatformId())) {
                        postShopBaseInfo.setPlatform(platformDomain.getPlatformName());
                        break;
                    }
                }
            }
            if (listCheckers != null && !listCheckers.isEmpty()) {
                // 审核员
                for (AccessUser accessUser : listCheckers) {
                    if (accessUser.getId().equals(postShop.getCheckerId())) {
                        postShopVo.setCheckerBaseInfo(
                                new UserBaseInfo()
                                        .setId(accessUser.getId())
                                        .setAvatar(accessUser.getAvatar())
                                        .setNickname(accessUser.getNickname()));
                        break;
                    }
                }
            }
            postShopVo.setPostShopBaseInfo(postShopBaseInfo);


            List<PostImg> imgList = CommonUtil.getItemsInResultObject(
                    postImgClient.getPostImgsByIdAndType(postShop.getId(), 1), PostImg.class);
            postShopVo.setPost(postShop).setPostImgList(imgList);
            voList.add(postShopVo);
        });
        return resultObject.changeList(voList);
    }

    @Override
    public ResultObject modifyPostStatus(PostShop postShop) {
        return postClient.updatePostShop(postShop);
    }


}
